< previous page page_511 next page >

Page 511
Adding Animation
Run the screen saver program in the Visual Basic IDE, and you see a blank screen until you press a key or move the mouse. This screen saver is not very exciting; it needs some animation, which can be added with the Timer control and some Image controls. The Timer control is covered in Chapter 4, "Using Visual Basic's Default Controls." Go ahead and add a simple animation now. For my screen saver, I created a car chase, as you can see in Figure 23.3.
sarrow.gifSee "Creating a Simple Animation" p. 83
0511-01.jpg
Figure 23.3
This screen saver form is shown in design
mode, with all the animation elements.
The code for such an animation is simple; just be sure to include a DoEvents statement so that the MouseMove event can be processed:
Dim CarX As Integer, CarY As Integer

Private Sub Form_Load()
If App.PrevInstance Then End
CheckCommandLine
CarX = Me.Width + 1000
CarY = 0
End Sub

Private Sub tmrMain_Timer()
    CarX = CarX - 35
    CarY = CarY + 13

    If CarX < LeftBorder - 1000 Then Form_Load

    imgCar.Top = CarY
    imgCar.Left = CarX
    imgBronco.Top = CarY + 230
    imgBronco.Left = CarX - 7000

    DoEvents
End Sub
Also, note that the program ends in the Load event if a previous instance occurs. This code is very important because Windows sometimes launches multiple instances of a running screen saver.

 
< previous page page_511 next page >